﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


///代码编写：丛兴滋（cncxz）  E-mail：cncxz@126.com
namespace Openfree.Framework.WebControl
{
	/// <summary>
	/// WebCalc 的摘要说明。
	/// </summary>
	[Description("网页计算器WebCalc控件")]
	[Designer(typeof(CalculatorDesigner))]
	[ToolboxData("<{0}:Calculator runat=server></{0}:Calculator>")]
	public class Calculator: Panel
	{
		public Calculator()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}

		protected override void Render(HtmlTextWriter writer) 
		{
			base.RenderBeginTag(writer);
			writer.Write(writer.NewLine);

			writer.Write(this.strScript());		//输出js脚本
			writer.Write(this.strFace());		//输出计算器界面
			writer.Write(writer.NewLine);

			base.RenderEndTag(writer);
		}

		private string strScript()
        {
			string scriptString ="\n";
			scriptString += @"<script language=""JavaScript"">"+"\n";
			scriptString += @"	<!--"+"\n";

			#region"	计算器js脚本	"
			scriptString += @"	var endNumber=true;var mem=0;var carry=10;var hexnum=""0123456789abcdef"";"+"\n";
			scriptString += @"	var angle=""d"";var stack="""";var level=""0"";var layer=0;"+"\n";
			scriptString += @"	var ResultboxID=""Result"";var MemoryboxID=""memory"";"+"\n\n";

			scriptString += @"	//数字键"+"\n";
			scriptString += @"function inputkey(key)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	var index=key.charCodeAt(0);"+"\n";
			scriptString += @"	if ((carry==2 && (index==48 || index==49))"+"\n";
			scriptString += @"	 || (carry==8 && index>=48 && index<=55)"+"\n";
			scriptString += @"	 || (carry==10 && (index>=48 && index<=57 || index==46))"+"\n";
			scriptString += @"	 || (carry==16 && ((index>=48 && index<=57) || (index>=97 && index<=102))))"+"\n";
			scriptString += @"	if(endNumber)"+"\n";
			scriptString += @"	{"+"\n";
			scriptString += @"		endNumber=false"+"\n";
			scriptString += @"		myResultbox.value = key"+"\n";
			scriptString += @"	}"+"\n";
			scriptString += @"	else if(myResultbox.value == null || myResultbox.value == ""0"")"+"\n";
			scriptString += @"		myResultbox.value = key"+"\n";
			scriptString += @"	else"+"\n";
			scriptString += @"		myResultbox.value += key"+"\n";
			scriptString += @"}"+"\n";

			scriptString += @"function changeSign()"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"    var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	if (myResultbox.value!=""0"")"+"\n";
			scriptString += @"    	if(myResultbox.value.substr(0,1) == ""-"")"+"\n";
			scriptString += @"        	myResultbox.value = myResultbox.value.substr(1)"+"\n";
			scriptString += @"    	else"+"\n";
			scriptString += @"        	myResultbox.value = ""-"" + myResultbox.value"+"\n";
			scriptString += @"}"+"\n\n";

			scriptString += @"//函数键"+"\n";
			scriptString += @"function inputfunction(fun,shiftfun)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	endNumber=true;"+"\n";
			scriptString += @"	var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	if (document.getElementById(""shiftf"").checked)"+"\n";
			scriptString += @"		myResultbox.value=decto(funcalc(shiftfun,(todec(myResultbox.value,carry))),carry)"+"\n";
			scriptString += @"	else"+"\n";
			scriptString += @"		myResultbox.value=decto(funcalc(fun,(todec(myResultbox.value,carry))),carry)"+"\n";
			scriptString += @"	document.getElementById(""shiftf"").checked=false"+"\n";
			scriptString += @"	document.getElementById(""hypf"").checked=false	"+"\n";
			scriptString += @"	inputshift()"+"\n";
			scriptString += @"}"+"\n";

			scriptString += @"function inputtrig(trig,arctrig,hyp,archyp)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	if (document.getElementById(""hypf"").checked)"+"\n";
			scriptString += @"		inputfunction(hyp,archyp)"+"\n";
			scriptString += @"	else"+"\n";
			scriptString += @"		inputfunction(trig,arctrig)"+"\n";
			scriptString += @"}"+"\n\n";


			
			scriptString += @"//运算符"+"\n";

			scriptString += @"function operation(join,newlevel)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	endNumber=true;"+"\n";
			scriptString += @"	var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	var temp=stack.substr(stack.lastIndexOf(""("")+1)+myResultbox.value"+"\n";
			scriptString += @"	while (newlevel!=0 && (newlevel<=(level.charAt(level.length-1))))"+"\n";
			scriptString += @"	{"+"\n";
			scriptString += @"		temp=parse(temp)"+"\n";
			scriptString += @"		level=level.slice(0,-1)"+"\n";
			scriptString += @"	}"+"\n";
			scriptString += @"	if (temp.match(/^(.*\d[\+\-\*\/\%\^\&\|x])?([+-]?[0-9a-f\.]+)$/))"+"\n";
			scriptString += @"		myResultbox.value=RegExp.$2"+"\n";
			scriptString += @"	stack=stack.substr(0,stack.lastIndexOf(""("")+1)+temp+join"+"\n";
			scriptString += @"	document.getElementById(""operator"").value="" ""+join+"" """+"\n";
			scriptString += @"	level=level+newlevel"+"\n";
	
			scriptString += @"}"+"\n";

			scriptString += @"//括号"+"\n";

			scriptString += @"function addbracket()"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	endNumber=true"+"\n";
			scriptString += @"	var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	myResultbox.value=0"+"\n";
			scriptString += @"	stack=stack+""("""+"\n";
			scriptString += @"	document.getElementById(""operator"").value=""   """+"\n";
			scriptString += @"	level=level+0"+"\n";
	
			scriptString += @"	layer+=1"+"\n";
			scriptString += @"	document.getElementById(""bracket"").value=""(=""+layer"+"\n";
			scriptString += @"}"+"\n";

			scriptString += @"function disbracket()"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	endNumber=true;"+"\n";
			scriptString += @"	var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	var temp=stack.substr(stack.lastIndexOf(""("")+1)+myResultbox.value"+"\n";
			scriptString += @"	while ((level.charAt(level.length-1))>0)"+"\n";
			scriptString += @"	{"+"\n";
			scriptString += @"		temp=parse(temp)"+"\n";
			scriptString += @"		level=level.slice(0,-1)"+"\n";
			scriptString += @"	}"+"\n";
	
			scriptString += @"	myResultbox.value=temp"+"\n";
			scriptString += @"	stack=stack.substr(0,stack.lastIndexOf(""(""))"+"\n";
			scriptString += @"	document.getElementById(""operator"").value=""   """+"\n";
			scriptString += @"	level=level.slice(0,-1)"+"\n";

			scriptString += @"	layer-=1"+"\n";
			scriptString += @"	if (layer>0)"+"\n";
			scriptString += @"		document.getElementById(""bracket"").value=""(=""+layer"+"\n";
			scriptString += @"	else"+"\n";
			scriptString += @"		document.getElementById(""bracket"").value="""""+"\n";
			scriptString += @"}"+"\n";

			scriptString += @"//等号"+"\n";

			scriptString += @"function result()"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	endNumber=true;"+"\n";
			scriptString += @"	var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	while (layer>0)"+"\n";
			scriptString += @"		disbracket()"+"\n";
			scriptString += @"	var temp=stack+myResultbox.value"+"\n";
			scriptString += @"	while ((level.charAt(level.length-1))>0)"+"\n";
			scriptString += @"	{"+"\n";
			scriptString += @"		temp=parse(temp)"+"\n";
			scriptString += @"		level=level.slice(0,-1)"+"\n";
			scriptString += @"	}"+"\n";

			scriptString += @"	myResultbox.value=temp"+"\n";
			scriptString += @"	document.getElementById(""bracket"").value="""""+"\n";
			scriptString += @"	document.getElementById(""operator"").value="""""+"\n";
			scriptString += @"	stack="""""+"\n";
			scriptString += @"	level=""0"""+"\n";
			scriptString += @"}"+"\n\n";


			
			scriptString += @"//修改键"+"\n";

			scriptString += @"function backspace()"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	if (!endNumber)"+"\n";
			scriptString += @"	{"+"\n";
			scriptString += @"		if(myResultbox.value.length>1)"+"\n";
			scriptString += @"			myResultbox.value=myResultbox.value.substring(0,myResultbox.value.length - 1)"+"\n";
			scriptString += @"		else"+"\n";
			scriptString += @"			myResultbox.value=0"+"\n";
			scriptString += @"	}"+"\n";
			scriptString += @"}"+"\n";

			scriptString += @"function clearall()"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	myResultbox.value=0"+"\n";
			scriptString += @"	endNumber=true"+"\n";
			scriptString += @"	stack="""""+"\n";
			scriptString += @"	level=""0"""+"\n";
			scriptString += @"	layer="""""+"\n";
			scriptString += @"	document.getElementById(""operator"").value="""""+"\n";
			scriptString += @"	document.getElementById(""bracket"").value="""""+"\n";
			scriptString += @"}"+"\n\n";


		
			scriptString += @"//转换键"+"\n";

			scriptString += @"function inputChangCarry(newcarry)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	endNumber=true"+"\n";
			scriptString += @"	myResultbox.value=(decto(todec(myResultbox.value,carry),newcarry))"+"\n";
			scriptString += @"	carry=newcarry"+"\n";

			scriptString += @"	document.getElementById(""sin"").disabled=(carry!=10)"+"\n";
			scriptString += @"	document.getElementById(""cos"").disabled=(carry!=10)"+"\n";
			scriptString += @"	document.getElementById(""tan"").disabled=(carry!=10)"+"\n";
			scriptString += @"	document.getElementById(""bt"").disabled=(carry!=10)"+"\n";
			scriptString += @"	document.getElementById(""pi"").disabled=(carry!=10)"+"\n";
			scriptString += @"	document.getElementById(""e"").disabled=(carry!=10)"+"\n";
			scriptString += @"	document.getElementById(""kp"").disabled=(carry!=10)"+"\n";
				
			scriptString += @"	document.getElementById(""k2"").disabled=(carry<=2)"+"\n";
			scriptString += @"	document.getElementById(""k3"").disabled=(carry<=2)"+"\n";
			scriptString += @"	document.getElementById(""k4"").disabled=(carry<=2)"+"\n";
			scriptString += @"	document.getElementById(""k5"").disabled=(carry<=2)"+"\n";
			scriptString += @"	document.getElementById(""k6"").disabled=(carry<=2)"+"\n";
			scriptString += @"	document.getElementById(""k7"").disabled=(carry<=2)"+"\n";
			scriptString += @"	document.getElementById(""k8"").disabled=(carry<=8)"+"\n";
			scriptString += @"	document.getElementById(""k9"").disabled=(carry<=8)"+"\n";
			scriptString += @"	document.getElementById(""ka"").disabled=(carry<=10)"+"\n";
			scriptString += @"	document.getElementById(""kb"").disabled=(carry<=10)"+"\n";
			scriptString += @"	document.getElementById(""kc"").disabled=(carry<=10)"+"\n";
			scriptString += @"	document.getElementById(""kd"").disabled=(carry<=10)"+"\n";
			scriptString += @"	document.getElementById(""ke"").disabled=(carry<=10)"+"\n";
			scriptString += @"	document.getElementById(""kf"").disabled=(carry<=10)"+"\n";	
	
			scriptString += @"}"+"\n";

			scriptString += @"function inputChangAngle(angletype)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	endNumber=true"+"\n";
			scriptString += @"	angle=angletype"+"\n";
			scriptString += @"	if (angle==""d"")"+"\n";
			scriptString += @"		myResultbox.value=radiansToDegress(myResultbox.value)"+"\n";
			scriptString += @"	else"+"\n";
			scriptString += @"		myResultbox.value=degressToRadians(myResultbox.value)"+"\n";
			scriptString += @"	endNumber=true"+"\n";
			scriptString += @"}"+"\n";

			scriptString += @"function inputshift()"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	if (document.getElementById(""shiftf"").checked)"+"\n";
			scriptString += @"	{"+"\n";
			scriptString += @"		document.getElementById(""bt"").value=""deg """+"\n";
			scriptString += @"		document.getElementById(""ln"").value=""exp """+"\n";
			scriptString += @"		document.getElementById(""log"").value=""expd"""+"\n";
		
			scriptString += @"		if (document.getElementById(""hypf"").checked)"+"\n";
			scriptString += @"		{"+"\n";
			scriptString += @"			document.getElementById(""sin"").value=""ahs """+"\n";
			scriptString += @"			document.getElementById(""cos"").value=""ahc """+"\n";
			scriptString += @"			document.getElementById(""tan"").value=""aht """+"\n";
			scriptString += @"		}"+"\n";
			scriptString += @"		else"+"\n";
			scriptString += @"		{"+"\n";
			scriptString += @"			document.getElementById(""sin"").value=""asin"""+"\n";
			scriptString += @"			document.getElementById(""cos"").value=""acos"""+"\n";
			scriptString += @"			document.getElementById(""tan"").value=""atan"""+"\n";
			scriptString += @"		}"+"\n";
		
			scriptString += @"		document.getElementById(""sqr"").value=""x^.5"""+"\n";
			scriptString += @"		document.getElementById(""cube"").value=""x^.3"""+"\n";
		
			scriptString += @"		document.getElementById(""floor"").value=""小数"""+"\n";
			scriptString += @"	}"+"\n";
			scriptString += @"	else"+"\n";
			scriptString += @"	{"+"\n";
			scriptString += @"		document.getElementById(""bt"").value=""d.ms"""+"\n";
			scriptString += @"		document.getElementById(""ln"").value="" ln """+"\n";
			scriptString += @"		document.getElementById(""log"").value=""log """+"\n";

			scriptString += @"		if (document.getElementById(""hypf"").checked)"+"\n";
			scriptString += @"		{"+"\n";
			scriptString += @"			document.getElementById(""sin"").value=""hsin"""+"\n";
			scriptString += @"			document.getElementById(""cos"").value=""hcos"""+"\n";
			scriptString += @"			document.getElementById(""tan"").value=""htan"""+"\n";
			scriptString += @"		}"+"\n";
			scriptString += @"		else"+"\n";
			scriptString += @"		{"+"\n";
			scriptString += @"			document.getElementById(""sin"").value=""sin """+"\n";
			scriptString += @"			document.getElementById(""cos"").value=""cos """+"\n";
			scriptString += @"			document.getElementById(""tan"").value=""tan """+"\n";
			scriptString += @"		}"+"\n";
		
			scriptString += @"		document.getElementById(""sqr"").value=""x^2 """+"\n";
			scriptString += @"		document.getElementById(""cube"").value=""x^3 """+"\n";
		
			scriptString += @"		document.getElementById(""floor"").value=""取整"""+"\n";
			scriptString += @"	}"+"\n";

			scriptString += @"}"+"\n\n";



			scriptString += @"//存储器部分"+"\n";

			scriptString += @"function clearmemory()"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	mem=0"+"\n";
			scriptString += @"	document.getElementById(MemoryboxID).value=""   """+"\n";
			scriptString += @"}"+"\n";

			scriptString += @"function getmemory()"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	endNumber=true"+"\n";
			scriptString += @"	myResultbox.value=decto(mem,carry)"+"\n";
			scriptString += @"}"+"\n";

			scriptString += @"function putmemory()"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	endNumber=true;"+"\n";
			scriptString += @"	if (myResultbox.value!=0)"+"\n";
			scriptString += @"	{"+"\n";
			scriptString += @"		mem=todec(myResultbox.value,carry)"+"\n";
			scriptString += @"		document.getElementById(MemoryboxID).value="" M """+"\n";
			scriptString += @"	}"+"\n";
			scriptString += @"	else"+"\n";
			scriptString += @"		document.getElementById(MemoryboxID).value=""   """+"\n";
			scriptString += @"}"+"\n";

			scriptString += @"function addmemory()"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	endNumber=true;"+"\n";
			scriptString += @"	mem=parseFloat(mem)+parseFloat(todec(myResultbox.value,carry))"+"\n";
			scriptString += @"	if (mem==0)"+"\n";
			scriptString += @"		document.getElementById(MemoryboxID).value=""   """+"\n";
			scriptString += @"	else"+"\n";
			scriptString += @"		document.getElementById(MemoryboxID).value="" M """+"\n";
			scriptString += @"}"+"\n";

			scriptString += @"function multimemory()"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	var myResultbox=document.getElementById(ResultboxID);"+"\n";
			scriptString += @"	endNumber=true;"+"\n";
			scriptString += @"	mem=parseFloat(mem)*parseFloat(todec(myResultbox.value,carry))"+"\n";
			scriptString += @"	if (mem==0)"+"\n";
			scriptString += @"		document.getElementById(MemoryboxID).value=""   """+"\n";
			scriptString += @"	else"+"\n";
			scriptString += @"		document.getElementById(MemoryboxID).value="" M """+"\n";
			scriptString += @"}"+"\n\n";



			scriptString += @"//十进制转换"+"\n";

			scriptString += @"function todec(num,oldcarry)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	if (oldcarry==10 || num==0) return(num)"+"\n";
			scriptString += @"	var neg=(num.charAt(0)==""-"")"+"\n";
			scriptString += @"	if (neg) num=num.substr(1)"+"\n";
			scriptString += @"	var newnum=0"+"\n";
			scriptString += @"	for (var index=1;index<=num.length;index++)"+"\n";
			scriptString += @"		newnum=newnum*oldcarry+hexnum.indexOf(num.charAt(index-1))"+"\n";
			scriptString += @"	if (neg)"+"\n";
			scriptString += @"		newnum=-newnum"+"\n";
			scriptString += @"	return(newnum)"+"\n";
			scriptString += @"}"+"\n";

			scriptString += @"function decto(num,newcarry)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	var neg=(num<0)"+"\n";
			scriptString += @"	if (newcarry==10 || num==0) return(num)"+"\n";
			scriptString += @"	num=""""+Math.abs(num)"+"\n";
			scriptString += @"	var newnum="""""+"\n";
			scriptString += @"	while (num!=0)"+"\n";
			scriptString += @"	{"+"\n";
			scriptString += @"		newnum=hexnum.charAt(num%newcarry)+newnum"+"\n";
			scriptString += @"		num=Math.floor(num/newcarry)"+"\n";
			scriptString += @"	}"+"\n";
			scriptString += @"	if (neg)"+"\n";
			scriptString += @"		newnum=""-""+newnum"+"\n";
			scriptString += @"	return(newnum)"+"\n";
			scriptString += @"}"+"\n\n";
			


			scriptString += @"//表达式解析"+"\n";

			scriptString += @"function parse(string)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	if (string.match(/^(.*\d[\+\-\*\/\%\^\&\|x\<])?([+-]?[0-9a-f\.]+)([\+\-\*\/\%\^\&\|x\<])([+-]?[0-9a-f\.]+)$/))"+"\n";
			scriptString += @"		return(RegExp.$1+cypher(RegExp.$2,RegExp.$3,RegExp.$4))"+"\n";
			scriptString += @"	else"+"\n";
			scriptString += @"		return(string)"+"\n";
			scriptString += @"}"+"\n";



			scriptString += @"//数学运算和位运算"+"\n";

			scriptString += @"function cypher(left,join,right)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	left=todec(left,carry)"+"\n";
			scriptString += @"	right=todec(right,carry)"+"\n";
			scriptString += @"	if (join==""+"")"+"\n";
			scriptString += @"		return(decto(parseFloat(left)+parseFloat(right),carry))"+"\n";
			scriptString += @"	if (join==""-"")"+"\n";
			scriptString += @"		return(decto(left-right,carry))"+"\n";
			scriptString += @"	if (join==""*"")"+"\n";
			scriptString += @"		return(decto(left*right,carry))"+"\n";
			scriptString += @"	if (join==""/"" && right!=0)"+"\n";
			scriptString += @"		return(decto(left/right,carry))"+"\n";
			scriptString += @"	if (join==""%"")"+"\n";
			scriptString += @"		return(decto(left%right,carry))"+"\n";
			scriptString += @"	if (join==""&"")"+"\n";
			scriptString += @"		return(decto(left&right,carry))"+"\n";
			scriptString += @"	if (join==""|"")"+"\n";
			scriptString += @"		return(decto(left|right,carry))"+"\n";
			scriptString += @"	if (join==""^"")"+"\n";
			scriptString += @"		return(decto(Math.pow(left,right),carry))"+"\n";
			scriptString += @"	if (join==""x"")"+"\n";
			scriptString += @"		return(decto(left^right,carry))"+"\n";
			scriptString += @"	if (join==""<"")"+"\n";
			scriptString += @"		return(decto(left<<right,carry))"+"\n";
			scriptString += @"	alert(""除数不能为零"")"+"\n";
			scriptString += @"	return(left)"+"\n";
			scriptString += @"}"+"\n\n";



			scriptString += @"//函数计算"+"\n";

			scriptString += @"function funcalc(fun,num)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	with(Math)"+"\n";
			scriptString += @"	{"+"\n";
			scriptString += @"		if (fun==""pi"")"+"\n";
			scriptString += @"			return(PI)"+"\n";
			scriptString += @"		if (fun==""e"")"+"\n";
			scriptString += @"			return(E)"+"\n";

			scriptString += @"		if (fun==""abs"")"+"\n";
			scriptString += @"			return(abs(num))"+"\n";
			scriptString += @"		if (fun==""ceil"")"+"\n";
			scriptString += @"			return(ceil(num))"+"\n";
			scriptString += @"		if (fun==""round"")"+"\n";
			scriptString += @"			return(round(num))"+"\n";

			scriptString += @"		if (fun==""floor"")"+"\n";
			scriptString += @"			return(floor(num))"+"\n";
			scriptString += @"		if (fun==""deci"")"+"\n";
			scriptString += @"			return(num-floor(num))"+"\n";


			scriptString += @"		if (fun==""ln"" && num>0)"+"\n";
			scriptString += @"			return(log(num))"+"\n";
			scriptString += @"		if (fun==""exp"")"+"\n";
			scriptString += @"			return(exp(num))"+"\n";
			scriptString += @"		if (fun==""log"" && num>0)"+"\n";
			scriptString += @"			return(log(num)*LOG10E)"+"\n";
			scriptString += @"		if (fun==""expdec"")"+"\n";
			scriptString += @"			return(pow(10,num))"+"\n";

			scriptString += @"		if (fun==""cube"")"+"\n";
			scriptString += @"			return(num*num*num)"+"\n";
			scriptString += @"		if (fun==""cubt"")"+"\n";
			scriptString += @"			return(pow(num,1/3))"+"\n";
			scriptString += @"		if (fun==""sqr"")"+"\n";
			scriptString += @"			return(num*num)"+"\n";
			scriptString += @"		if (fun==""sqrt"" && num>=0)"+"\n";
			scriptString += @"			return(sqrt(num))"+"\n";

			scriptString += @"		if (fun==""!"")"+"\n";
			scriptString += @"			return(factorial(num))"+"\n";

			scriptString += @"		if (fun==""recip"" && num!=0)"+"\n";
			scriptString += @"			return(1/num)"+"\n";
		
			scriptString += @"		if (fun==""dms"")"+"\n";
			scriptString += @"			return(dms(num))"+"\n";
			scriptString += @"		if (fun==""deg"")"+"\n";
			scriptString += @"			return(deg(num))"+"\n";

			scriptString += @"		if (fun==""~"")"+"\n";
			scriptString += @"			return(~num)"+"\n";
	
			scriptString += @"		if (angle==""d"")"+"\n";
			scriptString += @"		{"+"\n";
			scriptString += @"			if (fun==""sin"")"+"\n";
			scriptString += @"				return(sin(degressToRadians(num)))"+"\n";
			scriptString += @"			if (fun==""cos"")"+"\n";
			scriptString += @"				return(cos(degressToRadians(num)))"+"\n";
			scriptString += @"			if (fun==""tan"")"+"\n";
			scriptString += @"				return(tan(degressToRadians(num)))"+"\n";

			scriptString += @"			if (fun==""arcsin"" && abs(num)<=1)"+"\n";
			scriptString += @"				return(radiansToDegress(asin(num)))"+"\n";
			scriptString += @"			if (fun==""arccos"" && abs(num)<=1)"+"\n";
			scriptString += @"				return(radiansToDegress(acos(num)))"+"\n";
			scriptString += @"			if (fun==""arctan"")"+"\n";
			scriptString += @"				return(radiansToDegress(atan(num)))"+"\n";
			scriptString += @"		}"+"\n";
			scriptString += @"		else"+"\n";
			scriptString += @"		{"+"\n";
			scriptString += @"			if (fun==""sin"")"+"\n";
			scriptString += @"				return(sin(num))"+"\n";
			scriptString += @"			if (fun==""cos"")"+"\n";
			scriptString += @"				return(cos(num))"+"\n";
			scriptString += @"			if (fun==""tan"")"+"\n";
			scriptString += @"				return(tan(num))"+"\n";

			scriptString += @"			if (fun==""arcsin"" && abs(num)<=1)"+"\n";
			scriptString += @"				return(asin(num))"+"\n";
			scriptString += @"			if (fun==""arccos"" && abs(num)<=1)"+"\n";
			scriptString += @"				return(acos(num))"+"\n";
			scriptString += @"			if (fun==""arctan"")"+"\n";
			scriptString += @"				return(atan(num))"+"\n";
			scriptString += @"		}"+"\n";
	
			scriptString += @"		if (fun==""hypsin"")"+"\n";
			scriptString += @"			return((exp(num)-exp(0-num))*0.5)"+"\n";
			scriptString += @"		if (fun==""hypcos"")"+"\n";
			scriptString += @"			return((exp(num)+exp(-num))*0.5)"+"\n";
			scriptString += @"		if (fun==""hyptan"")"+"\n";
			scriptString += @"			return((exp(num)-exp(-num))/(exp(num)+exp(-num)))"+"\n";

			scriptString += @"		if (fun==""ahypsin"" | fun==""hypcos"" | fun==""hyptan"")"+"\n";
			scriptString += @"		{"+"\n";
			scriptString += @"			alert(""对不起,公式还没有查到!"")"+"\n";
			scriptString += @"			return(num)"+"\n";
			scriptString += @"		}"+"\n";
		
			scriptString += @"		alert(""超出函数定义范围"")"+"\n";
			scriptString += @"		return(num)"+"\n";
			scriptString += @"	}"+"\n";
			scriptString += @"}"+"\n\n";

			scriptString += @"function factorial(n)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	n=Math.abs(parseInt(n))"+"\n";
			scriptString += @"	var fac=1"+"\n";
			scriptString += @"	for (;n>0;n-=1)"+"\n";
			scriptString += @"		fac*=n"+"\n";
			scriptString += @"	return(fac)"+"\n";
			scriptString += @"}"+"\n";

			scriptString += @"function dms(n)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	var neg=(n<0)"+"\n";
			scriptString += @"	with(Math)"+"\n";
			scriptString += @"	{"+"\n";
			scriptString += @"		n=abs(n)"+"\n";
			scriptString += @"		var d=floor(n)"+"\n";
			scriptString += @"		var m=floor(60*(n-d))"+"\n";
			scriptString += @"		var s=(n-d)*60-m"+"\n";
			scriptString += @"	}"+"\n";
			scriptString += @"	var dms=d+m/100+s*0.006"+"\n";
			scriptString += @"	if (neg) "+"\n";
			scriptString += @"		dms=-dms"+"\n";
			scriptString += @"	return(dms)"+"\n";
			scriptString += @"}"+"\n";
		
			scriptString += @"function deg(n)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	var neg=(n<0)"+"\n";
			scriptString += @"	with(Math)"+"\n";
			scriptString += @"	{"+"\n";
			scriptString += @"		n=abs(n)"+"\n";
			scriptString += @"		var d=floor(n)"+"\n";
			scriptString += @"		var m=floor((n-d)*100)"+"\n";
			scriptString += @"		var s=(n-d)*100-m"+"\n";
			scriptString += @"	}"+"\n";
			scriptString += @"	var deg=d+m/60+s/36"+"\n";
			scriptString += @"	if (neg) "+"\n";
			scriptString += @"		deg=-deg"+"\n";
			scriptString += @"	return(deg)"+"\n";
			scriptString += @"}"+"\n";

			scriptString += @"function degressToRadians(degress)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	return(degress*Math.PI/180)"+"\n";
			scriptString += @"}"+"\n";

			scriptString += @"function radiansToDegress(radians)"+"\n";
			scriptString += @"{"+"\n";
			scriptString += @"	return(radians*180/Math.PI)"+"\n";
			scriptString += @"}"+"\n";
			#endregion

			scriptString += @"//-->"+"\n";
			scriptString += @"<";
			scriptString += @"/";
			scriptString += @"script>"+"\n";

			return scriptString;
		}

		private string strFace()
        {
			string faceString="";
			faceString+=@"	<table border=""0"" cellpadding=""0"" cellspacing=""0"" class=""Calc"">"+"\n";
			faceString+=@"                              <tr>"+"\n";
			faceString+=@"                                <td><table width=""100%""  border=""0"" cellspacing=""0"" cellpadding=""0"" class=""calc_Result"">"+"\n";
			faceString+=@"                                  <tr>"+"\n";
			faceString+=@"                                    <td class=""left"">结果：</td>"+"\n";
			faceString+=@"                                    <td class=""right""><input type=""text"" id=""Result"" name=""Result"" value=""0"" readonly size=""40""></td>"+"\n";
			faceString+=@"                                  </tr>"+"\n";
			faceString+=@"                                </table>"+"\n";
			faceString+=@"                                  <table width=""100%"" class=""calc_Top""  border=""0"" cellspacing=""0"" cellpadding=""0"">"+"\n";
			faceString+=@"                                    <tr>"+"\n";
			faceString+=@"                                      <td class=""left""><input type=""radio"" name=""carry"" onClick=""inputChangCarry(16)"">十六进制"+"\n";
			faceString+=@"									  		<input type=""radio"" name=""carry"" checked onClick=""inputChangCarry(10)"">十进制"+"\n";
			faceString+=@"											<input type=""radio"" name=""carry"" onClick=""inputChangCarry(8)"">八进制"+"\n";
			faceString+=@"									  <input type=""radio"" name=""carry"" onClick=""inputChangCarry(2)"">二进制</td>"+"\n";
			faceString+=@"                                      <td class=""right""><input type=""radio"" name=""angle"" value=""d"" onClick=""inputChangAngle('d')"" checked>角度制"+"\n";
			faceString+=@"							  		  <input type=""radio"" name=""angle"" value=""r"" onClick=""inputChangAngle('r')"">弧度制</td>"+"\n";
			faceString+=@"                                    </tr>"+"\n";
			faceString+=@"                                  </table> "+"\n";

			faceString+=@"                                  <table width=""100%"" class=""calc_Center"" border=""0"" cellspacing=""0"" cellpadding=""0"">"+"\n";
			faceString+=@"                                    <tr>"+"\n";
			faceString+=@"                                      <td class=""left""><input name=""shiftf"" type=""checkbox"" onClick=""inputshift()"">上档功能"+"\n";
			faceString+=@"									  <input name=""hypf"" type=""checkbox"" onClick=""inputshift()"">双曲函数</td>"+"\n";
			faceString+=@"                                      <td class=""center"">"+"\n";
			faceString+=@"                                        	<input name=""bracket"" type=""text"" size=""3"" readonly>"+"\n";
			faceString+=@"                                            <input name=""memory"" type=""text"" size=""3"" readonly>"+"\n";
			faceString+=@"                                            <input name=""operator"" type=""text"" size=""3"" readonly>"+"\n";
			faceString+=@"									  </td>"+"\n";
			faceString+=@"                                      <td class=""right""><input name=""button0"" type=""button"" onClick=""backspace()"" value="" 退格 "">"+"\n";
			faceString+=@"                                        <input name=""button1"" type=""button"" onClick=""document.getElementById('Result').value = 0 "" value="" 清屏 "">"+"\n";
			faceString+=@"                                        <input name=""button2"" type=""button"" onClick=""clearall()"" value="" 全清""></td>"+"\n";
			faceString+=@"                                    </tr>"+"\n";
			faceString+=@"                                  </table>"+"\n";


			
			faceString+=@"                                  <table width=""100%"" class=""calc_Bottom"" border=""0"" cellspacing=""0"" cellpadding=""0"">"+"\n";
			faceString+=@"                                    <tr>"+"\n";
			faceString+=@"                                      <td class=""left""><table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0"">"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""pi"" type=""button"" onClick=""inputfunction('pi','pi')"" value="" PI ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""e"" type=""button"" onClick=""inputfunction('e','e')"" value="" E  ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""bt"" type=""button"" onClick=""inputfunction('dms','deg')"" value=""d.ms""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""button3"" type=""button"" onClick=""addbracket()"" value="" (  ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button4"" type=""button"" onClick=""disbracket()"" value="" )  ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""ln"" type=""button"" onClick=""inputfunction('ln','exp')"" value="" ln ""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""sin"" type=""button"" onClick=""inputtrig('sin','arcsin','hypsin','ahypsin')"" value=""sin ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button5"" type=""button"" onClick=""operation('^',7)"" value=""x^y ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""log"" type=""button"" onClick=""inputfunction('log','expdec')"" value=""log ""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""cos"" type=""button"" onClick=""inputtrig('cos','arccos','hypcos','ahypcos')"" value=""cos ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""cube"" type=""button"" onClick=""inputfunction('cube','cubt')"" value=""x^3 ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button6"" type=""button"" onClick=""inputfunction('!','!')"" value="" n! ""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""tan"" type=""button"" onClick=""inputtrig('tan','arctan','hyptan','ahyptan')"" value=""tan ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""sqr"" type=""button"" onClick=""inputfunction('sqr','sqrt')"" value=""x^2 ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button"" type=""button"" onClick=""inputfunction('recip','recip')"" value=""1/x ""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                      </table></td>"+"\n";

			faceString+=@"                                      <td class=""center""><table width=""100%"" class=""center"" border=""0"" cellspacing=""0"" cellpadding=""0"">"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""button7"" type=""button"" onClick=""putmemory()"" value="" 储存 ""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""button8"" type=""button"" onClick=""getmemory()"" value="" 取存 ""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""button9"" type=""button"" onClick=""addmemory()"" value="" 累存 ""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""button10"" type=""button"" onClick=""multimemory()"" value="" 积存 ""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""button11"" type=""button"" onClick=""clearmemory()"" value="" 清存 ""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                      </table></td>"+"\n";

			
			faceString+=@"                                     <td class=""right""><table width=""100%"" class=""right"" border=""0"" cellspacing=""0"" cellpadding=""0"">"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""k7"" type=""button"" value="" 7 "" onClick=""inputkey('7')""></td>"+"\n";
			faceString+=@"                                          <td><input name=""k8"" type=""button"" value="" 8 "" onClick=""inputkey('8')""></td>"+"\n";
			faceString+=@"                                          <td><input name=""k9"" type=""button"" value="" 9 "" onClick=""inputkey('9')""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button15"" type=""button"" onClick=""operation('/',6)"" value="" / ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button25"" type=""button"" onClick=""operation('%',6)"" value=""取余""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button24"" type=""button"" onClick=""operation('&amp;',3)"" value="" 与 ""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""k4"" type=""button"" value="" 4 "" onClick=""inputkey('4')""></td>"+"\n";
			faceString+=@"                                          <td><input name=""k5"" type=""button"" value="" 5 "" onClick=""inputkey('5')""></td>"+"\n";
			faceString+=@"                                          <td><input name=""k6"" type=""button"" value="" 6 "" onClick=""inputkey('6')""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button16"" type=""button"" onClick=""operation('*',6)"" value="" * ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""floor"" type=""button"" onClick=""inputfunction('floor','deci')""	value=""取整""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button23"" type=""button"" onClick=""operation('|',1)"" value="" 或 ""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""button13"" type=""button"" onClick=""inputkey('1')"" value="" 1 ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""k2"" type=""button"" value="" 2 "" onClick=""inputkey('2')""></td>"+"\n";
			faceString+=@"                                          <td><input name=""k3"" type=""button"" value="" 3 "" onClick=""inputkey('3')""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button17"" type=""button"" onClick=""operation('-',5)"" value="" - ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button21"" type=""button"" onClick=""operation('<',4)"" value=""左移""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button22"" type=""button"" onClick=""inputfunction('~','~')"" value="" 非 ""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""button12"" type=""button"" onClick=""inputkey('0')"" value="" 0 ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button14"" type=""button"" onClick=""changeSign()"" value=""+/-""></td>"+"\n";
			faceString+=@"                                          <td><input name=""kp"" type=""button"" value="" . "" onClick=""inputkey('.')""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button18"" type=""button"" onClick=""operation('+',5)"" value="" + ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button19"" type=""button"" onClick=""result()"" value="" ＝ ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""button20"" type=""button"" onClick=""operation('x',2)"" value=""异或""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                        <tr>"+"\n";
			faceString+=@"                                          <td><input name=""ka"" type=""button"" disabled onClick=""inputkey('a')"" value="" A ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""kb"" type=""button"" disabled onClick=""inputkey('b')"" value="" B ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""kc"" type=""button"" disabled onClick=""inputkey('c')"" value="" C ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""kd"" type=""button"" disabled onClick=""inputkey('d')"" value="" D ""></td>"+"\n";
			faceString+=@"                                          <td><input name=""ke"" type=""button"" disabled onClick=""inputkey('e')"" value="" E""></td>"+"\n";
			faceString+=@"                                          <td><input name=""kf"" type=""button"" disabled onClick=""inputkey('f')"" value="" F""></td>"+"\n";
			faceString+=@"                                        </tr>"+"\n";
			faceString+=@"                                      </table></td>"+"\n";
			faceString+=@"                                    </tr>"+"\n";
			faceString+=@"                                  </table></td>"+"\n";
			faceString+=@"                              </tr>"+"\n";
			faceString+=@"                            </table>"+"\n";

			return faceString;
		}
	}

	/// <summary>
	/// 网页计算器webcalc控件设计器。
	/// </summary>
	public class CalculatorDesigner:System.Web.UI.Design.WebControls.PanelDesigner
	{
        private Calculator Wcalc;

        public CalculatorDesigner()
		{
			this.ReadOnly=true;
		}

		public override string GetDesignTimeHtml()
		{

            Wcalc = (Calculator)Component;			
			StringWriter sw=new StringWriter();
			HtmlTextWriter writer=new HtmlTextWriter(sw);
			Wcalc.RenderControl(writer);
			
			string myStr=@"";
			myStr+=@"<table style=""background-color:#fffffe; border:1px solid #A2D0FF; line-height:120%;"" width=""240"" border=""0"" cellspacing=""2"" cellpadding=""2"">";
			myStr+=@"  <tr>";
			myStr+=@"    <td height=""24"" style="" font-size:14px; letter-spacing:2px; text-indent:5px; border-bottom:1px solid #dddddd;FILTER: progid:DXImageTransform.Microsoft.Gradient(startColorStr='#fafafa', endColorStr='#f0f0f0', gradientType='0');"">WebCalc风格设置说明</td>";
			myStr+=@"  </tr><tr>";
			myStr+=@"    <td height=""40"" style="" font-size:12px; padding:4px 4px 0px 4px; "">&nbsp;&nbsp;&nbsp;&nbsp;本控件外观由css控制，请在&lt;head&gt;标签内加入如下代码（具体风格可参照下载得来的css文件自行修改）：</td>";
			myStr+=@"  </tr><tr>";
			myStr+=@"    <td height=""30"" style="" font-size:12px; padding:0px 4px 4px 4px;; color:#FF0000; "">&lt;LINK href=&quot;cssUrl&quot; type=&quot;text/css&quot; rel=&quot;stylesheet&quot;&gt;</td>";
			myStr+=@"  </tr></table>";
			
			return myStr;		
		}

		/// <summary>
		/// 获取在呈现控件时遇到错误后在设计时为指定的异常显示的 HTML。
		/// </summary>
		/// <param name="e">要为其显示错误信息的异常。</param>
		/// <returns>设计时为指定的异常显示的 HTML。</returns>
		protected override string GetErrorDesignTimeHtml(Exception e)
		{
			string errorstr="创建控件时出错1："+e.Message;
			return CreatePlaceHolderDesignTimeHtml(errorstr);
		}
	}
}